local skels = {}

---@param player Player
local get_skill_xiqu = function(player)
  local list = {}
    for _, name in ipairs(player:getSkillNameList()) do
    if name:startsWith("lb_zero") and name:endsWith("__xiqu") then
      table.insert(list, {name, player:usedSkillTimes(name, Player.HistoryGame)})
    end
  end
  return list
end

Fk:addQmlMark {
  name = "lb_zero__xiqu",
  how_to_show = function(name, value, player)
    local list = get_skill_xiqu(player)
    return #list == 0 and "#hidden" or tostring(#list)
  end,
  qml_data = function(name, value, player)
    return get_skill_xiqu(player)
  end,
  qml_path = "packages/lb_kill_utility/qml/SkillMarkDetailBox"
}

Fk:loadTranslationTable {
  ["lb_zero__xiqu"] = "锡躯",
  [":lb_zero__xiqu"] = "<b>限定技</b>，出牌阶段，你可以展示一名角色的一张手牌，令其选择一项：此牌不可被没有〖锡躯〗的角色使用；重铸此牌，令你从弃牌堆获得一张【杀】。",

  ["@@lb_zero__xiqu"] = "锡躯",
  ["#lb_zero__xiqu-skill"] = "锡躯：请选择一项！",
  ["lb_zero__xiqu__seal"] = "令此牌不能被没有锡躯的角色使用",
  ["lb_zero__xiqu__recast"] = "重铸此牌，令对方获得一张【杀】",

  ["@[lb_zero__xiqu]lb_zero__xiqu"] = "锡躯",

  ["$lb_zero__xiqu1"] = "绝不撤退。",
  ["$lb_zero__xiqu2"] = "听从命令。",
}

for loop = 0, 110 do
  local skel = fk.CreateSkill {
    name = loop == 0 and "lb_zero__xiqu" or "lb_zero" .. loop .. "__xiqu",
    --tags = { Skill.Limited },
  }

  skel:addEffect("active", {
    mute = true,
    card_num = 0,
    target_num = 1,
    times = function(self, player)
      return 1 - player:usedSkillTimes(skel.name, Player.HistoryGame)
    end,
    can_use = function(self, player)
      return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
    end,
    card_filter = Util.FalseFunc,
    target_filter = function(self, player, to_select, selected, selected_cards)
      return #selected == 0 and not to_select:isKongcheng()
    end,
    on_use = function(self, room, effect)
      local player = effect.from
      local target = effect.tos[1]
      player:broadcastSkillInvoke("lb_zero__xiqu")
      --[[
      room:doAnimate("InvokeSkill", {
        name = "lb_zero__xiqu",
        player = player.id,
        skill_type = "offensive",
      })
      --]]
      room:notifySkillInvoked(player, "lb_zero__xiqu", "offensive")
      local id = room:askToChooseCard(player, {
        target = target,
        flag = "h",
        skill_name = skel.name,
      })
      player:showCards({ id })
      if table.contains(target:getCardIds("h"), id) and not target.dead then
        local choices = { "lb_zero__xiqu__seal", "lb_zero__xiqu__recast" }
        local choice = room:askToChoice(target, {
          choices = choices,
          skill_name = skel.name,
        })
        if choice == "lb_zero__xiqu__recast" then
          room:recastCard({ id }, target, skel.name)
          if not player.dead then
            local cards = room:getCardsFromPileByRule("slash", 1, "discardPile")
            if #cards > 0 then
              room:obtainCard(player, cards, true, fk.ReasonJustMove, player, "lb_zero__xiqu")
            end
            --[[
            local get = {}
            local names = {"slash", "fire__slash", "thunder__slash", "ice__slash"}
            if not Fk.all_card_types["ice__slash"] then --军争一般都开吧？
              table.removeOne(names, "ice__slash")
            end
            if #names > 0 then
              table.insert(get, room:printCard(table.random(names, 1)[1], math.random(5), math.random(0, 14)).id)
              room:obtainCard(player, get, true, fk.ReasonJustMove, player, self.name, MarkEnum.DestructIntoDiscard)
            end
            --]]
          end
        elseif choice == "lb_zero__xiqu__seal" then
          room:setCardMark(Fk:getCardById(id), "@@lb_zero__xiqu", effect.from.id)
        end
      end
    end,
  })

  skel:addLoseEffect(function(self, player, is_death)
    player:setSkillUseHistory(skel.name, 0, Player.HistoryGame)
    player:doNotify("LoseSkill", json.encode { player.id, skel.name, true })
  end)

  local function xiqu(player)
    for i = 0, 110, 1 do
      local skill = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
      if player:hasSkill(skill, true) then
        return true
      end
    end
  end

  skel:addEffect("prohibit", {
    prohibit_use = function(self, player, card)
      local cards = card:isVirtual() and card.subcards or { card.id }
      return table.find(cards, function(id) return Fk:getCardById(id):getMark("@@lb_zero__xiqu") ~= 0 end)
          and not xiqu(player)
    end,
  })

  skel:addEffect(fk.AfterCardsMove, {
    can_refresh = function(self, event, target, player, data)
      for _, move in ipairs(data) do
        if move.from then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@lb_zero__xiqu") ~= 0 then
              return true
            end
          end
        end
      end
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      --[[data.extra_data = data.extra_data or {}
      data.extra_data.lb_zero__xiqu = data.extra_data.lb_zero__xiqu or {}]]
      for _, move in ipairs(data) do
        if move.from then
          for _, info in ipairs(move.moveInfo) do
            local owner = Fk:getCardById(info.cardId):getMark("@@lb_zero__xiqu")
            if info.fromArea == Card.PlayerHand and owner ~= 0 then
              room:setCardMark(Fk:getCardById(info.cardId), "@@lb_zero__xiqu", 0)
              --[[for _, p in ipairs(room:getAllPlayers()) do
                  local mark = p:getTableMark("lb_zero__wanwo")
                  if #mark > 0 then
                    table.removeOne(mark, info.cardId)
                    room:setPlayerMark(p, "lb_zero__wanwo", #mark > 0 and mark or 0)
                  end
                end
              if move.moveReason == fk.ReasonDiscard then
                data.extra_data.lb_zero__xiqu[owner] = 1 --多次同时弃置暂定一张印杀算了
              end]]
            end
          end
        end
      end
    end,
  })

  skel:addAcquireEffect(function (self, player, is_start)
    player.room:setPlayerMark(player, "@[lb_zero__xiqu]lb_zero__xiqu", 1)
  end)

  skel:addLoseEffect(function(self, player, is_death)
    if not (player:hasSkill("lb_zero__yishan", true) or player:hasSkill("lb_zero__wanwo", true)) then
      if not xiqu(player) then
        player.room:setPlayerMark(player, "@[lb_zero__xiqu]lb_zero__xiqu", 0)
      end
    end
  end)

  Fk:loadTranslationTable {
    ["lb_zero" .. loop .. "__xiqu"] = "锡躯",
    [":lb_zero" .. loop .. "__xiqu"] = "<b>限定技</b>，出牌阶段，你可以展示一名角色的一张手牌，令其选择一项：此牌不可被没有〖锡躯〗的角色使用；重铸此牌，令你获得一张【杀】。",
  }

  table.insert(skels, skel)
end

return skels
